<?php
namespace Api\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $this->show('hello world','utf-8');
    }

    /**
     * 二维码地址 - 为了能动态控制地址的改变
     */
    public function qrgoto(){
        return redirect("http://pucha.getpv.com/?v=1.0");

    }
    


    /**
     * 
     * 随机获取10条问题
     * 
     */
    public function question(){

     
        $all_question = 'all_question_cache_name';

        if(S($all_question)){
            //有缓存的时候
            $list = S($all_question);
        }else{         
            //无缓存的时候 
            $list = M("question")->order('rand()')->field("id,title,img,zhengque_xuhao,jiedu")->limit(30)->select();
            // $sql = $Question->getLastSql();
            // var_dump($sql);

            foreach($list as &$val){
                $options = M("option")->field("name,xuhao,q_id")->where(array('q_id'=>array('eq',$val['id'])))->order("xuhao asc")->select();
                $val['options'] = $options;
               // $val['img'] = 'http://'.$_SERVER['SERVER_NAME'].$val['img']; //绝对路径
            }

            //var_dump($list);exit; 
            S($all_question,$list,7200);
        }

        shuffle($list);//大乱数组


        //随机取10条数组
        $list  = array_slice($list,0,10);
     


        //var_dump($list);exit;

        echo return_ajax($list);

    }




    //抽奖接口
    public function choujiang_0522(){

        $cj_mobile =  I('param.mobile','');//手机号
        if(!isMobile($cj_mobile)){
            $data = array(
                'code'=>-1,
                'msg'=>'手机号格式不正确'
             );
             echo return_ajax($data);
             exit;
        }
       




        /**
         * 每天固定奖品 U盘20, 科普书15本，达到后不在重奖
         * 重奖的手机号不能再重奖了
         */
        $jiangpin =array('u盘','科普书');

 
        $beginToday=date("Y-m-d". " 00:00:00");
        $endToday=date("Y-m-d",strtotime("+1 day")).' 00:00:00';
        // var_dump($beginToday);
        // var_dump($endToday);



         //每个手机每日可以抽奖5次
         $map = array(
            'cj_mobile'=>array("eq",$cj_mobile),
            'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
         );
         $todayMobile = M("choujiang")->where($map)->count();
         if($todayMobile >=5){
            $data = array(
                'code'=>-1,
                'msg'=>'您今日已经抽奖5次喽！<br />请明天继续参与'
             );
             echo return_ajax($data);
             exit;
         }


        //重奖的不再抽奖
        $map = array(
        'mobile'=>array("eq",$cj_mobile), 
        );
        $mobile_have_choujiang = M("choujiang")->where($map)->count();
        if($mobile_have_choujiang>0){ 
            $data = array(
                'code'=>-1,
                'msg'=>'您已经中过奖喽！把机会留给别人吧'
            );
            echo return_ajax($data);
            exit;
        }



         //今天的u盘重奖的数量
         $map = array(
            'jpname'=>array("eq",$jiangpin[0]),
            'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
         );
         $todayUpan = M("choujiang")->where($map)->count();
         //var_dump($todayUpan);exit;


         //今天的科普书重奖的数量
         $map = array(
            'jpname'=>array("eq",$jiangpin[1]),
            'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
         );
         $todayKepushu = M("choujiang")->where($map)->count();
         //var_dump($todayKepushu);exit;


         //达到上限，不再抽奖。
         if($todayUpan>=20 and $todayKepushu>=15){
             //达到奖品的上限了，不再抽中
             $data = array(
                'code'=>-10,
                'msg'=>'很遗憾，未抽中奖品！'
             );
             echo return_ajax($data);
             exit;

         }else{
           

            //抽奖
            $luckNum = rand(1,10);
            //$luckNum=10;//调试
            if($luckNum>3){          

                $jpname = array_rand($jiangpin);
                $jpname = $jiangpin[$jpname];

                //判断今日奖品是否达到上限
                $map = array(
                    'jpname'=>array("eq",$jpname),
                    'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
                );
                $full = M("choujiang")->where($map)->count();
                if($jpname=='u盘' and $full>=20){
                    $data = array(
                        'code'=>-10,
                        'msg'=>'很遗憾，未抽中奖品！', 
                    );
                    echo return_ajax($data);
                    exit;
                }elseif($jpname=='科普书' and $full>=15){
                    $data = array(
                        'code'=>-10,
                        'msg'=>'很遗憾，未抽中奖品！', 
                    );
                    echo return_ajax($data);
                    exit;
                }



                $ip = get_client_ip();
                $choujiang_ma = rand(10000,90000);//抽奖码
                M("choujiang")->add(array(                    
                    'ip'=>$ip, 
                    'cj_mobile'=>$cj_mobile, 
                    'jpname'=>$jpname,
                    'choujiang_ma'=>$choujiang_ma,
                    'status'=>-1, 
                    'addtime'=>date("Y-m-d H:i:s")
                ));
                
                $msg = '恭喜您，重奖奖品：'.$jpname;
                if($jpname ==$jiangpin[0]){
                    $msg = '恭喜您获得定制U盘';
                }elseif($jpname == $jiangpin[1]){
                    $msg = '恭喜您获得统计科普书籍';
                }

                $data = array(
                    'code'=>0,
                    'msg'=>$msg,
                    'choujiang_ma'=>$choujiang_ma
                );
                echo return_ajax($data);
             
            }


            $data = array(
                'code'=>-10,
                'msg'=>'很遗憾，未抽中奖品！', 
            );
            echo return_ajax($data);


         }

         

    }


    //抽奖接口
    public function choujiang(){

        $cj_mobile =  I('param.mobile','');//手机号
        if(!isMobile($cj_mobile)){
            $data = array(
                'code'=>-2,
                'msg'=>'手机号格式不正确'
             );
             echo return_ajax($data);
             exit;
        }
       




        /**
         * 每天固定奖品50个，达到后不在重奖
         * 重奖的手机号不能再重奖了
         */
         
        $jiangpin =array('随机奖品');

 
        $beginToday=date("Y-m-d". " 00:00:00");
        $endToday=date("Y-m-d",strtotime("+1 day")).' 00:00:00';
        // var_dump($beginToday);
        // var_dump($endToday);



         //每个手机每日可以抽奖5次
         $map = array(
            'cj_mobile'=>array("eq",$cj_mobile),
            'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
         );
         $todayMobile = M("choujiang")->where($map)->count();
         if($todayMobile >=5){
            $data = array(
                'code'=>-1,
                'msg'=>'您今日已经抽奖5次喽！<br />请明天继续参与'
             );
             echo return_ajax($data);
             exit;
         }


        //重奖的不再抽奖
        $map = array(
        'mobile'=>array("eq",$cj_mobile), 
        );
        $mobile_have_choujiang = M("choujiang")->where($map)->count();
        if($mobile_have_choujiang>0){ 
            $data = array(
                'code'=>-1,
                'msg'=>'您已经中过奖喽！把机会留给别人吧'
            );
            echo return_ajax($data);
            exit;
        }



         //今天重奖的数量
         $map = array(
            'jpname'=>array("eq",$jiangpin[0]),
            'status'=>0,//中奖的，填写地址的。
            'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
         );
         $todayUpan = M("choujiang")->where($map)->count();
         //var_dump($todayUpan);exit;

         //达到上限，不再抽奖。
         if($todayUpan>=50 ){
             //达到奖品的上限了，不再抽中
             $data = array(
                'code'=>-10,
                'msg'=>'很遗憾，未抽中奖品！'
             );
             echo return_ajax($data);
             exit;

         }else{

            // $prize_arr = array(
            // '0' => array('id'=>1,'prize'=>$jiangpin[0],'v'=>1),  //百分之一      
            // '1' => array('id'=>6,'prize'=>'很遗憾，未抽中奖品！','v'=>99),
            // );
           
            // 抽奖结束日期
            $dt = 1591372800;//2020-06-06 00:00:00 
            //抽奖
            $luckNum = rand(1,1000);
            //$luckNum=10;//调试
            if($luckNum>995 and time() < $dt){          

                $jpname = array_rand($jiangpin);
                $jpname = $jiangpin[$jpname];

                //判断今日奖品是否达到上限
                $map = array(
                    'jpname'=>array("eq",$jpname),
                    'status'=>0,
                    'addtime'=>array('BETWEEN',array($beginToday,$endToday)),
                );
                $full = M("choujiang")->where($map)->count();
                if($full>=50){
                    $data = array(
                        'code'=>-10,
                        'msg'=>'很遗憾，未抽中奖品！', 
                    );
                    echo return_ajax($data);
                    exit;
                } 


                $ip = get_client_ip();
                //$choujiang_ma = rand(10000,90000);//抽奖码
                $choujiang_ma = time();//抽奖码
                M("choujiang")->add(array(                    
                    'ip'=>$ip, 
                    'cj_mobile'=>$cj_mobile, 
                    'jpname'=>$jpname,
                    'choujiang_ma'=>$choujiang_ma,
                    'status'=>-1, 
                    'addtime'=>date("Y-m-d H:i:s")
                ));
                
                $msg = '恭喜您，抽中奖品！';
           

                $data = array(
                    'code'=>0,
                    'msg'=>$msg,
                    'choujiang_ma'=>$choujiang_ma
                );
                echo return_ajax($data);
             
            }


            $data = array(
                'code'=>-10,
                'msg'=>'很遗憾，未抽中奖品！', 
            );
            echo return_ajax($data);


         }

         

    }

    //领奖接口
    public function linjiang(){
        
        $mobile =  I('param.mobile','');//手机号
        $name =  I('param.name','');//姓名
        $address =  I('param.address','');//地址
        $choujiang_ma =  I('param.choujiang_ma','');//抽奖码
        
        if(!isMobile($mobile)){
            $data = array(
                'code'=>-2,
                'msg'=>'手机号格式不正确'
             );
             echo return_ajax($data);
             exit;
        }
        if(!$name){
            $data = array(
                'code'=>-2,
                'msg'=>'请填写姓名'
             );
             echo return_ajax($data);
             exit;
        }

        if(!$address){
            $data = array(
                'code'=>-2,
                'msg'=>'请填写地址'
             );
             echo return_ajax($data);
             exit;
        }


         //重奖的不再抽奖
         $map = array(
            'mobile'=>array("eq",$mobile), 
        );
        $mobile_have_choujiang = M("choujiang")->where($map)->count();
        if($mobile_have_choujiang>0){ 
            $data = array(
                'code'=>-1,
                'msg'=>'您已经中过奖喽！<br />把机会留给别人吧'
            );
            echo return_ajax($data);
            exit;
        }
          



        //判断抽奖码是否有效
        $map = [
            'choujiang_ma'=>['eq',$choujiang_ma],
            'status'=>['eq',-1]
        ];
        $havejiangpin = M("choujiang")->where($map)->find();

        if(!$havejiangpin){
            $data = array(
                'code'=>-2,
                'msg'=>'抽奖码无效'
             );
             echo return_ajax($data);
             exit;
        }

        //更新信息
        $rs = M("choujiang")->where($map)->save([
            'status'=>0,
            'mobile'=>$mobile,
            'name'=>$name,
            'address'=>$address, 
        ]);

        if($rs){
             $data = array(
                'code'=>0,
                'msg'=>'提交成功！请静候奖品的到来吧!'
             );
             echo return_ajax($data);
             exit;
        }else{
            $data = array(
                'code'=>0,
                'msg'=>'提交异常！请稍后再试'
             );
             echo return_ajax($data);
             exit;
        }


    }


    /**
     * 答题统计
     */
    public function dati(){

        $score =  I('param.score','');//分数
        $ip = get_client_ip();
        $rs =  M("dati")->add(array(
            'score'=>$score,            
            'ip'=>$ip,         
            'addtime'=>date("Y-m-d H:i:s")
        ));

         //达到奖品的上限了，不再抽中
         $data = array(
            'code'=>0,
            'msg'=>'答题统计成功'
        );
        echo return_ajax($data);
        exit;
     

    }



}